﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyBanHang.QuerySQL;
using QuanLyBanHang.DataConnection;

namespace QuanLyBanHang
{
    public partial class frMain : Office2007RibbonForm
    {
        Connect conn = new Connect();
        //bool dn;
        String tk = null;
        String pass = null;
        String quyen = null;
        String manv = null;
        DataTable tb = new DataTable();
        ClassLogin lg = new ClassLogin();
        //Form
        frLogin frmLogin = null;

        public frMain()
        {
            InitializeComponent();
        }
        //sự kiện tạo phim tắt(sk ẩn)
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case (Keys.F1):
                    btndangnhap_Click(null, null);
                    break;
                case (Keys.F2):
                    btndangxuat_Click(null, null);
                    break;
                case (Keys.F3):
                    btndoimatkhau_Click(null, null);
                    break;
                case (Keys.F4):
                    btnthoat_Click(null, null);
                    break;
                case (Keys.F5):
                    btnItemGT_Click(null, null);
                    break;
                case (Keys.F6):
                    btngiupdo_Click(null, null);
                    break;
                //case (Keys.Control | Keys.P):
                //    BtnTest5_Click(null, null);
                //    break;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }  
        //
        private void login_default(string quyen)
        {
            switch (quyen)
            {
                case null: {
                    btnItemND.Enabled = false;
                    btnItemNV.Enabled = false;
                    btnItemKH.Enabled = false;//
                    btnItemSP.Enabled = false;
                    btnItemLSP.Enabled = false;
                    btnItemNSX.Enabled = false;//
                    btnItemHDBH.Enabled = false;
                    btnItemQLHD.Enabled = false;
                    btnItemDH.Enabled = false;
                    btnItemQLDH.Enabled = false;//
                    btnItemPN.Enabled = false;
                    btnItemQLPN.Enabled = false;
                    btnItemNCC.Enabled = false;//
                    btnItemTKSP.Enabled = false;
                    btnItemTKDT.Enabled = false;
                    break;
                }
                case "quantri":
                    {
                        btnItemND.Enabled = true;
                        btnItemNV.Enabled = true;
                        btnItemKH.Enabled = true;//
                        btnItemSP.Enabled = true;
                        btnItemLSP.Enabled = true;
                        btnItemNSX.Enabled = true;//
                        btnItemHDBH.Enabled = true;
                        btnItemQLHD.Enabled = true;
                        btnItemDH.Enabled = true;
                        btnItemQLDH.Enabled = true;//
                        btnItemPN.Enabled = true;
                        btnItemQLPN.Enabled = true;
                        btnItemNCC.Enabled = true;//
                        btnItemTKSP.Enabled = true;
                        btnItemTKDT.Enabled = true;
                        break;
                    }
                case "quanly":
                    {
                        btnItemND.Enabled = false;
                        btnItemNV.Enabled = true;
                        btnItemKH.Enabled = true;//
                        btnItemSP.Enabled = true;
                        btnItemLSP.Enabled = true;
                        btnItemNSX.Enabled = true;//
                        btnItemHDBH.Enabled = true;
                        btnItemQLHD.Enabled = true;
                        btnItemDH.Enabled = true;
                        btnItemQLDH.Enabled = true;//
                        btnItemPN.Enabled = true;
                        btnItemQLPN.Enabled = true;
                        btnItemNCC.Enabled = true;//
                        btnItemTKSP.Enabled = true;
                        btnItemTKDT.Enabled = true;
                        break;
                    }
                case "nhanvien":
                    {
                        btnItemND.Enabled = false;
                        btnItemNV.Enabled = false;
                        btnItemKH.Enabled = false;//
                        btnItemSP.Enabled = true;
                        btnItemLSP.Enabled = true;
                        btnItemNSX.Enabled = true;//
                        btnItemHDBH.Enabled = true;
                        btnItemQLHD.Enabled = false;
                        btnItemDH.Enabled = true;
                        btnItemQLDH.Enabled = false;//
                        btnItemPN.Enabled = true;
                        btnItemQLPN.Enabled = false;
                        btnItemNCC.Enabled = true;//
                        btnItemTKSP.Enabled = true;
                        btnItemTKDT.Enabled = true;
                        break;
                    }
            }
        }
        /*Phương thức CheckExistForm kiểm tra 1 form nào đó đã tồn tại trong form cha chưa
       thuộc tính MdiChildren trả về tập các form con đang hiên thị trong form cha(tên form)*/
        public bool CheckExistForm(string name)
        {
            bool check = false;
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Name == name)
                {
                    check = true;
                    break;
                }
            }
            return check;
        }

        /*Phương thức ActiveChildForm Hiển thị lên trên cung form con(có thể k cần vì dùng tabstrip)*/
        private void ActiveChildForm(string name)
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Name == name)
                {
                    frm.Activate();
                    break;
                }

            }
        }
        private void btnItemND_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frNguoidung"))
            {
                frNguoidung frND = new frNguoidung();
                frND.MdiParent = this;
                frND.Show();
            }
            else
            {
                ActiveChildForm("frNguoidung");
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            DialogResult kq = MessageBox.Show("Chắc chưa ?","Thoát chương trình", MessageBoxButtons.YesNo);
            if (kq == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btnItemNV_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frNhanvien"))
            {
                frNhanvien frNV = new frNhanvien();
                frNV.MdiParent = this;
                frNV.Show();
            }
            else
            {
                ActiveChildForm("frNhanvien");
            }
        }

        private void btngiupdo_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Cứ từ từ, không đi đâu mà vội ...","Thông báo");
        }

        private void btnItemKH_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frKhachhang"))
            {
                frKhachhang frmKH = new frKhachhang();
                frmKH.MdiParent = this;
                frmKH.Show();
            }
            else
            {
                ActiveChildForm("frKhachhang");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Hour > 12)
            {
                lbtime.Text = DateTime.Now.Hour.ToString() + " : " + DateTime.Now.Minute.ToString() + " : " + DateTime.Now.Second.ToString() + " PM";
            }
            else
            {
                lbtime.Text = DateTime.Now.Hour.ToString() + " : " + DateTime.Now.Minute.ToString() + " : " + DateTime.Now.Second.ToString() + " AM";
            }
        }

        private void frMain_Load(object sender, EventArgs e)
        {
            // Tạo con tro chuột
            if (conn.checkConn())
            {
                //con tro
                this.Cursor = MyCursors.Create(System.IO.Path.Combine(Application.StartupPath, "Pointer.cur"));
                login_default(quyen);
            }
            else
            {
                login_default(quyen);
                MessageBox.Show("Loi ket noi CSDL - kiem tra file Connection.xml trong thu muc debug", "Thông báo");
            }
        }
        //Login
        public bool login(String id, String pass)
        {
            bool i = false;
            tb = lg.ShowTK(id, pass);
            if (tb.Rows.Count != 0)
            {                
                foreach (DataRow row in tb.Rows)
                {
                    this.tk = row["ID"].ToString();
                    this.quyen = row["MAQUYEN"].ToString();
                    this.manv = row["MANV"].ToString();
                    this.pass = row["PASS"].ToString();
                }
                i = true;
            }
            return i;
        }
        public void loginS()
        {
        Cout:
            {
                if (frmLogin == null || frmLogin.IsDisposed)
                {
                    frmLogin = new frLogin();
                }
            }
            if (frmLogin.ShowDialog() == DialogResult.OK)
            {

                if (frmLogin.txttaikhoan.Text == "")
                {
                    frmLogin.lbthongbao.Text = "Chưa nhập Tài khoản...";
                    goto Cout;
                }
                else
                {
                    if (frmLogin.txtpasss.Text == "")
                    {
                        frmLogin.lbthongbao.Text = "Chưa nhập mật khẩu...";
                        goto Cout;
                    }
                }
                bool i = login(frmLogin.txttaikhoan.Text, frmLogin.txtpasss.Text);
                switch (i)
                {
                    case false: { frmLogin.lbthongbao.Text = "Sai tài khoản hay mật khẩu..."; goto Cout; }
                    case true: 
                        {
                            MessageBox.Show("Bạn đăng nhập với quyền '" + quyen + "'", "Thông báo");
                            frmLogin.lbthongbao.Text="";
                            //Kiểm tra xem có ghi nhớ tài khoản k
                            if(!frmLogin.ckbghinho.Checked){
                                frmLogin.txttaikhoan.Clear();
                                frmLogin.txtpasss.Clear();
                            }
                            break;
                        }
                    default:
                        break;
                }

            }

        }
        private void btndangnhap_Click(object sender, EventArgs e)
        {
            if (quyen == null)
            {
                loginS();
                frMain_Load(sender, e);
            }
            else {
                MessageBox.Show("Phải thực hiện đăng xuất!!!", "Thông báo");
            }
        }

        private void btnItemSP_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frSanpham"))
            {
                frSanpham frmSP = new frSanpham();
                frmSP.MdiParent = this;
                frmSP.Show();
            }
            else
            {
                ActiveChildForm("frSanpham");
            }
        }

        private void btndangxuat_Click(object sender, EventArgs e)
        {
            if (tk == null)
            {
                MessageBox.Show("Chưa đăng nhập!!!", "Thông báo");
            }
            else
            {
                DialogResult kq = MessageBox.Show("Bạn có chắc muốn đăng xuất ?", "Thông báo", MessageBoxButtons.YesNo);
                if (kq == DialogResult.Yes)
                {
                    this.quyen = null;
                    this.tk = null;
                    this.pass = null;
                    this.manv = null;
                    frMain_Load(sender, e);
                    foreach (Form mdiForm in this.MdiChildren)
                        mdiForm.Close();
                }
            }
        }
        
        private void timer2_Tick(object sender, EventArgs e)
        {

            //lbgioithieu.Text = lbgioithieu.Text.Substring(lbgioithieu.Text.Length - 1, 1) +"  " +lbgioithieu.Text.Substring(0, lbgioithieu.Text.Length - 1);
            string ch = lbgioithieu.Text;
            lbgioithieu.Text = lbgioithieu.Text.Substring(1, lbgioithieu.Text.Length - 1);
            lbgioithieu.Text += ch.Substring(0, 1);
        }

        private void btndoimatkhau_Click(object sender, EventArgs e)
        {
            if (tk == null)
            {
                MessageBox.Show("Chưa đăng nhập!!!","Thông báo");
            }
            else {
                frDoimatkhau frmDMK = new frDoimatkhau();
                frmDMK.txttaikhoan.Text = tk;
                frmDMK.matkhau = pass;
                frmDMK.ShowDialog();
                pass = frmDMK.pass;
            }
        }

        private void btnItemLSP_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frLoaisp"))
            {
                frLoaisp frmLSP = new frLoaisp();
                frmLSP.MdiParent = this;
                frmLSP.Show();
            }
            else
            {
                ActiveChildForm("frLoaisp");
            }
        }

        private void btnItemNSX_Click(object sender, EventArgs e)
        {
            if(!CheckExistForm("frNhasx"))
            {
                frNhasx frmNSX = new frNhasx();
                frmNSX.MdiParent = this;
                frmNSX.Show();
            }
            else
            {
                ActiveChildForm("frNhasx");
            }
        }

        private void btnItemHDBH_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frHoadonban"))
            {
                frHoadonban frmHDB = new frHoadonban();
                frmHDB.nhanvien = manv;
                frmHDB.quyen = quyen;
                frmHDB.MdiParent = this;
                frmHDB.Show();
            }
            else
            {
                ActiveChildForm("frHoadonban");
            }
        }

        private void btnItemQLHD_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frqlHoadonbh"))
            {
                frqlHoadonbh frmqlHDB = new frqlHoadonbh();
                frmqlHDB.MdiParent = this;
                frmqlHDB.Show();
            }
            else
            {
                ActiveChildForm("frqlHoadonbh");
            }
        }

        private void btnItemDH_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frDondathang"))
            {
                frDondathang frmDDH = new frDondathang();
                frmDDH.quyen = quyen;
                frmDDH.nhanvien = manv;
                frmDDH.MdiParent = this;
                frmDDH.Show();
            }
            else
            {
                ActiveChildForm("frDondathang");
            }
        }

        private void btnItemQLDH_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frqlDathang"))
            {
                frqlDathang frmqlDH = new frqlDathang();
                //frmDDH.quyen = quyen;
                //frmDDH.nhanvien = manv;
                frmqlDH.MdiParent = this;
                frmqlDH.Show();
            }
            else
            {
                ActiveChildForm("frqlDathang");
            }
        }

        private void btnItemPN_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frHoadonnhap"))
            {
                frHoadonnhap frmhdn = new frHoadonnhap();
                frmhdn.nhanvien = manv;
                frmhdn.quyen = quyen;
                frmhdn.MdiParent = this;
                frmhdn.Show();
            }
            else
            {
                ActiveChildForm("frHoadonnhap");
            }
        }

        private void btnItemQLPN_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frqlNhaphang"))
            {
                frqlNhaphang frm = new frqlNhaphang();
                frm.MdiParent = this;
                frm.Show();
            }
            else
            {
                ActiveChildForm("frqlNhaphang");
            }
        }

        private void btnItemNCC_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frNhacc"))
            {
                frNhacc frm = new frNhacc();
                frm.MdiParent = this;
                frm.Show();
            }
            else
            {
                ActiveChildForm("frNhacc");
            }
        }

        private void btnItemGT_Click(object sender, EventArgs e)
        {
            coder cd = new coder();
            cd.ShowDialog();
        }

        private void btnItemTKDT_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frThongkeDT"))
            {
                frThongkeDT frm = new frThongkeDT();
                frm.MdiParent = this;
                frm.Show();
            }
            else
            {
                ActiveChildForm("frThongkeDT");
            }
        }
        private void btnItemTKSP_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frThongkesp"))
            {
                frThongkesp frm = new frThongkesp();
                frm.MdiParent = this;
                frm.Show();
            }
            else
            {
                ActiveChildForm("frThongkesp");
            }
        }
    }
}
